package com.jiedeng.controller;

import com.jiedeng.dao.*;
import com.jiedeng.entity.PersonDetails;
import com.jiedeng.mapper.PersonMapper;
import com.jiedeng.model.*;
import com.jiedeng.util.DatetimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @author zhouyb
 * @ClassName: PersonController
 * @Description:
 * @date 2017/5/24 14:03
 */
@RestController
@RequestMapping("/person")
public class PersonController {
    @Autowired
    private PersonMapper personMapper;

    @Autowired
    IPersonRepository personRepository;
    @Autowired
    ICorpPostRepository corpPostRepository;
    @Autowired
    ICorpRepository corpRepository;

    /**
     * 查询人员列表
     *
     * @param corpId
     * @param page
     * @param pageSize
     * @return
     */
    @RequestMapping("personCountList")
    public List<PersonDetails> personCountList(String corpId, String page, String pageSize) {
        if (StringUtils.isEmpty(page) || StringUtils.isEmpty(pageSize)) {
            page = "1";
            pageSize = "10";
        }
        List<PersonDetails> list = personMapper.personCountList(corpId, page, pageSize);
        return list;
    }

    /**
     * 查询人员列表总数
     *
     * @return
     */
    @RequestMapping("personCountNum")
    public int personCountNum(String corpId) {
        int count = personMapper.personCountNum(corpId);
        return count;
    }

    /**
     * 查询人员编辑列表
     *
     * @return
     */
    @RequestMapping("personEdit")
    public List<PersonDetails> personEdit(String personId) {
        List<PersonDetails> list = personMapper.personEdit(personId);
        return list;
    }

    /**
     * 查询人员编辑列表
     *
     * @return
     */
    @RequestMapping("findPersonById")
    public Person findPersonById(String personId) {
        Person person = new Person();
        Optional<Person> optional = personRepository.findById(Long.valueOf(personId));
        if (optional.isPresent()) {
            person = optional.get();
        }
        return person;
    }

    /**
     * 查询人员编辑列表
     *
     * @return
     */
    @RequestMapping("findPersonByName")
    public int findPersonByName(String loginid) {
        Person person = new Person();
        List<Person> optional = personRepository.findByLoginid(loginid);
        if (optional != null && optional.size() > 0) {
            person = optional.get(0);
            return optional.size();
        }
        return 0;
    }

    /**
     * 新增人员
     *
     * @param name 姓名
     * @param loginid 账号
     * @param password 密码
     * @param phone 号码
     * @param isLock 是否锁
     * @param corpId 单位id
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param addTime 注册时间
     * @return rst
     */
    @RequestMapping("addPersonEntity")
    public Rst addPersonEntity(String name, String loginid, String password, String phone,
                               String isLock, String corpId, String startTime, String endTime, String addTime, String postId) {
        Rst rst = new Rst();
        try {
            Person person = new Person();
            person.setName(name);
            person.setLoginid(loginid);
            person.setPassword(password);
            person.setPhone(phone);
            person.setMobileValue("&CameraParam:default&;");
            person.setPostId(Long.valueOf(postId));
            if ("1".equals(isLock)) {
                person.setLock(true);
            } else {
                person.setLock(false);
            }
            person.setCorpId(Long.valueOf(corpId));
            person.setStartTime(DatetimeUtil.formartDate("yyyy-MM-dd HH:mm:ss", startTime, 0));
            person.setAddTime(new Date());
            if (!StringUtils.isEmpty(endTime)) {
                person.setEndTime(DatetimeUtil.formartDate("yyyy-MM-dd HH:mm:ss", endTime, 0));
            }
            personRepository.save(person);
            rst.setResultCode(0);
            rst.setMsg("OK");
        } catch (Exception e) {
            rst.setResultCode(1);
            rst.setMsg("Mysql Error");
        }
        return rst;
    }

    /**
     * 修改用户
     *
     * @param name 姓名
     * @param loginid 账号
     * @param password 密码
     * @param phone 号码
     * @param isLock 是否锁
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param postId  岗位
     * @param personId 人员id
     * @return rst
     */
    @RequestMapping("editPersonEntity")
    public Rst editPersonEntity(String name, String loginid, String password, String phone,
                                String isLock, String personId, String startTime, String endTime, String postId) {
        Rst rst = new Rst();
        try {
            Person person = personRepository.getOne(Long.valueOf(personId));
            //person.setName(name);
            person.setLoginid(loginid);
            person.setPassword(password);
            person.setPhone(phone);
            person.setPostId(Long.valueOf(postId));
            if ("1".equals(isLock)) {
                person.setLock(true);
            } else {
                person.setLock(false);
            }
            person.setStartTime(DatetimeUtil.formartDate("yyyy-MM-dd HH:mm:ss", startTime, 0));
            //person.setAddTime(DatetimeUtil.formartDate("yyyy-MM-dd HH:mm:ss", startTime, 0));
            if (!StringUtils.isEmpty(endTime)) {
                person.setEndTime(DatetimeUtil.formartDate("yyyy-MM-dd HH:mm:ss", endTime, 0));
            }
            personRepository.save(person);
            rst.setResultCode(0);
            rst.setMsg("OK");
        } catch (Exception e) {
            rst.setResultCode(1);
            rst.setMsg("Mysql Error");
        }
        return rst;
    }

    /**
     * 根据单位获取岗位
     *
     * @return
     */
    @RequestMapping(value = "/findCorpPostByCorpId", method = RequestMethod.GET)
    public List<CorpPost> findCorpPostByCorpId(long corpId) {
        return corpPostRepository.findAll();
        /*Optional<Corp> optional = corpRepository.findById(corpId);
        long corpPropId=0;
        if (optional.isPresent()) {
            Corp corp = optional.get();
            corpPropId=corp.getCorpPropId();
        }
        long startId = 0;
        long endId = 0;
        //建设管理单位岗位
        if (corpPropId == 2) {
            startId = 9200;
            endId = 9300;
        }else
        //监理单位岗位
        if (corpPropId == 4) {
            startId = 9400;
            endId = 9500;
        }else
        //施工单位岗位
        if (corpPropId == 5) {
            startId = 9500;
            endId = 9600;
        }else{
            return corpPostRepository.findAll();
            //return corpPostRepository.findByCorpId(corpId);
        }
        List<CorpPost> buildCorpPosts = corpPostRepository.findByIdBetween(startId, endId);
        return buildCorpPosts;*/
    }
}
